package com.ssy.invoicing.login.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ssy.invoicing.common.controller.BaseController;
import com.ssy.invoicing.invouser.bean.InvoUser;
import com.ssy.invoicing.invouser.service.InvoUserService;


@Controller
@RequestMapping("invoicing")
public class LoginController extends BaseController {
    
    @Resource(name="invoUserService")
    private InvoUserService invoUserService;

    @RequestMapping("login")
    public ModelAndView login() {
        return new ModelAndView("login/index");
    }
    
    @RequestMapping("signup")
    public ModelAndView signup(String name, String pwd, ModelMap model,HttpServletRequest request) {
        InvoUser invoUser = invoUserService.getInvoUserByName(name);
        if (invoUser == null) {
            model.put("message", "用户不存在");
            model.put("name", name);
            return new ModelAndView("login/index");
        }
        //比对密码
        if (!pwd.equals(invoUser.getPwd())) {
            model.put("message", "用户密码错误");
            model.put("name", name);
            return new ModelAndView("login/index");
        }
        request.getSession().setAttribute("user", invoUser);
        return new ModelAndView("frame/index");
    }
    
    @RequestMapping("topFrame")
    public ModelAndView topFrame() {
        return new ModelAndView("frame/top");
    }
    
    @RequestMapping("leftFrame")
    public ModelAndView leftFrame() {
        return new ModelAndView("frame/left");
    }
    
    @RequestMapping("mainFrame")
    public ModelAndView mainFrame() {
        return new ModelAndView("frame/main");
    }
    
    @RequestMapping("signout")
    public ModelAndView signout(HttpServletRequest request) {
        request.getSession().removeAttribute("user");
        return new ModelAndView("login/index");
    }
    
}
